package com.winderinfo.yikaotianxia.ui.address;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.winderinfo.yikaotianxia.R;
import com.winderinfo.yikaotianxia.ui.record.RecordBean;

import java.util.List;

public class AddressAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context context;
    private List<AddresListBean.RowsBean> list;

    public AddressAdapter(Context context, List<AddresListBean.RowsBean> list) {
        this.context = context;
        this.list = list;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_addresslist, viewGroup, false);
        SelectedViewHolder selectedViewHolder = new SelectedViewHolder(view);
        return selectedViewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, final int i) {
        SelectedViewHolder selectedViewHolder = (SelectedViewHolder) viewHolder;
        selectedViewHolder.iadd_name.setText(list.get(i).getName());
        selectedViewHolder.iadd_phone.setText(list.get(i).getPhone());
        selectedViewHolder.iadd_address.setText(list.get(i).getAddr());
        if (list.get(i).getIsdefault() == "1") {
            selectedViewHolder.iadd_moren.setVisibility(View.VISIBLE);
        }
        selectedViewHolder.iadd_del.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int id = list.get(i).getId();
                if (onaddDel != null) {
                    onaddDel.delId(id);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public class SelectedViewHolder extends RecyclerView.ViewHolder {

        private final TextView iadd_name, iadd_phone, iadd_address, iadd_del, iadd_moren;

        public SelectedViewHolder(@NonNull View itemView) {
            super(itemView);
            iadd_name = itemView.findViewById(R.id.iadd_name);
            iadd_phone = itemView.findViewById(R.id.iadd_phone);
            iadd_address = itemView.findViewById(R.id.iadd_address);
            iadd_del = itemView.findViewById(R.id.iadd_del);
            iadd_moren = itemView.findViewById(R.id.iadd_moren);
        }
    }


    public interface OnaddDel {
        void delId(int id);
    }

    public OnaddDel onaddDel;

    public void setOnaddDel(OnaddDel onaddDel) {
        this.onaddDel = onaddDel;
    }


}
